/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package replic.catalog;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

/**
 *
 * @author Leonardo Oliveira Moreira
 * 
 * Class used to read the contents of the XML catalog
 */
public class Catalog {

    private File catalog;
    public List<VM> virtualMachineList;

    public Catalog(File catalog) throws IOException {
        this.catalog = catalog;
        virtualMachineList = getVirtualMachineList();

    }

    /**
     * Method used to retrieve a list of virtual machines described in XML catalog
     * @return 
     */
    private List<VM> getVirtualMachineList() throws IOException {
        List<VM> result = new ArrayList<VM>();
        SAXBuilder builder = new SAXBuilder();
        Document document = null;
        try {
            document = builder.build(catalog);
            List vmList = document.getRootElement().getChildren();
            for (int i = 0; vmList != null && i < vmList.size(); i++) {
                Element vm = (Element) vmList.get(i);
                VM vmTemp = new VM();
                vmTemp.setName(vm.getChildText("name"));
                vmTemp.setHost(vm.getChildText("host"));
                vmTemp.setPort(Integer.parseInt(vm.getChildText("port")));
                List dbList = vm.getChild("db_list").getChildren();
                List<DB> dbListTemp = new ArrayList<DB>();
                for (int j = 0; dbList != null && j < dbList.size(); j++) {
                    Element db = (Element) dbList.get(j);
                    DB dbTemp = new DB();
                    dbTemp.setVmName(vmTemp.getName());
                    dbTemp.setDbDriver(db.getChildText("db_driver"));
                    dbTemp.setDbName(db.getChildText("db_name"));
                    dbTemp.setDbUrl(db.getChildText("db_url"));
                    dbTemp.setDbUser(db.getChildText("db_user"));
                    dbTemp.setDbPassword(db.getChildText("db_password"));
                    dbListTemp.add(dbTemp);
                }
                vmTemp.setDbList(dbListTemp);
                result.add(vmTemp);
            }
        } catch (JDOMException ex) {
            throw new IOException(ex.getMessage());
        }
        return result;
    }

    public List<DB> getDatabaseListByName(String name) {
        List<DB> result = new ArrayList<DB>();
        List<VM> vmList = getVirtualMachineListByDatabase(name);
        for (VM vm : vmList) {
            for (DB db : vm.getDbList()) {
                if (db.getDbName().equalsIgnoreCase(name)) {
                    result.add(db);
                    break;
                }
            }
        }
        return result;
    }

    public List<VM> getVirtualMachineListByDatabase(String dbName) {
        List<VM> result = new ArrayList<VM>();
        List<VM> vmList = null;
        try {
            vmList = getVirtualMachineList();
        } catch (IOException ex) {
            return result;
        }
        for (VM vm : vmList) {
            boolean found = false;
            for (DB db : vm.getDbList()) {
                if (db.getDbName().equalsIgnoreCase(dbName)) {
                    found = true;
                    break;
                }
            }
            if (found) {
                result.add(vm);
            }
        }
        return result;
    }

    public List<DB> getDatabaseList() {
        List<DB> result = new ArrayList<DB>();
        List<VM> vmList = null;
        try {
            vmList = getVirtualMachineList();
        } catch (IOException ex) {
            return result;
        }
        for (VM vm : vmList) {
            for (DB db : vm.getDbList()) {
                result.add(db);
            }
        }
        return result;
    }
}